package org.lifxue.jqda;

import java.awt.Font;
import java.util.Date;
import java.util.Enumeration;
import java.util.Timer;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import org.lifxue.jqda.jintellitype.GlobalHotkeyResourceManagement;
import org.lifxue.jqda.run.IndexTask;
import org.lifxue.jqda.ui.TrayPopupMenu;
import org.lifxue.jqda.ui.util.UIUtil;
import org.lifxue.jqda.util.GlobalVar;
import org.lifxue.jqda.util.InstanceRunning;

/**
 * 
 * @Description:
 * @Author: zhangzuoqiang
 * @Date: Sep 26, 2011
 */
public class App {

	// private static Log logger = LogFactory.getLog(App.class);
	public static Timer APP_Timer;

	public App() {
		// 初始化系统变量
		GlobalVar.init();
		InstanceRunning.isInstanceRunning();// 判断单进程

		// 系统热键
		GlobalHotkeyResourceManagement.initResources();
		GlobalHotkeyResourceManagement.addListener();
	}

	public static void initGlobalFontSetting(Font fnt) {
		FontUIResource fontRes = new FontUIResource(fnt);
		for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys
				.hasMoreElements();) {
			Object key = keys.nextElement();
			Object value = UIManager.get(key);
			if (value instanceof FontUIResource){
				UIManager.put(key, fontRes);
			}
		}
	}

	public static void main(String[] args) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		initGlobalFontSetting(new Font("微软雅黑",Font.PLAIN,12)); 
		
		new App();

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				UIUtil.setUI();// 设置LOOK_AND_FEEL
				TrayPopupMenu trayPopupMenu = new TrayPopupMenu();
				trayPopupMenu.tray();
			}
		});

		App.APP_Timer = new Timer(true);
		IndexTask indexTask = new IndexTask(GlobalVar.INDEX_SCAN_DIR,
				GlobalVar.INDEX_SCAN_TYPE);

		long period = 0;
		long delay = 0;
		if (GlobalVar.INDEX_CYCLE.equals("每天一次")) {
			period = 24 * 60 * 60 * 1000;
		} else if (GlobalVar.INDEX_CYCLE.equals("每周一次")) {
			period = 7 * 24 * 60 * 60 * 1000;
		} else if (GlobalVar.INDEX_CYCLE.equals("每旬一次")) {
			period = 10 * 24 * 60 * 60 * 1000;
		} else if (GlobalVar.INDEX_CYCLE.equals("每月一次")) {
			period = 30 * 24 * 60 * 60 * 1000;
		} else if (GlobalVar.INDEX_CYCLE.equals("每季一次")) {
			period = 3 * 30 * 24 * 60 * 60 * 1000;
		}
		Date d = new Date();
		long cha = d.getTime() - Long.parseLong(GlobalVar.INDEX_LASTTIME);
		if (cha >= period) {
			delay = 0;
		} else {
			delay = period - cha;
		}

		App.APP_Timer.schedule(indexTask, delay, period);
	}
}
